package com.zqxx.examine.management.controller.page.admin;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.zqxx.examine.common.utils.Page;
import com.zqxx.examine.common.utils.PagingUtil;
import com.zqxx.examine.management.security.UserInfo;
import com.zqxx.examine.management.service.NewsService;
import com.zqxx.examine.management.service.UserService;
import com.zqxx.examine.model.news.News;
import com.zqxx.examine.model.user.Department;
import com.zqxx.examine.model.user.Role;
import com.zqxx.examine.model.user.User;

@Controller
public class SystemPageAdmin
{
  @Autowired
  private UserService userService;
  @Autowired
  private NewsService newsService;
  
  @RequestMapping(value={"/admin/system/admin-list"}, method={org.springframework.web.bind.annotation.RequestMethod.GET})
  private String adminListPage(Model model, HttpServletRequest request)
  {
    int index = 1;
    if (request.getParameter("page") != null) {
      index = Integer.parseInt(request.getParameter("page"));
    }
    Page<User> page = new Page();
    page.setPageNo(index);
    page.setPageSize(10);
    UserInfo userInfo = (UserInfo)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    List<Department> depList = this.userService.getDepList(null);
    List<User> userList = this.userService.getUserListByRoleId(((Role)userInfo.getRoleMap().get("ROLE_ADMIN")).getRoleId(), page);
    String pageStr = PagingUtil.getPagelink(index, page.getTotalPage(), "", "admin/system/admin-list");
    model.addAttribute("depList", depList);
    model.addAttribute("userList", userList);
    model.addAttribute("pageStr", pageStr);
    return "sys-admin-list";
  }
  
  @RequestMapping(value={"/admin/system/admin-add"}, method={org.springframework.web.bind.annotation.RequestMethod.GET})
  private String adminAddPage(Model model, HttpServletRequest request)
  {
    return "";
  }
  
  @RequestMapping(value={"/admin/system/backup"}, method={org.springframework.web.bind.annotation.RequestMethod.GET})
  private String backupPage(Model model, HttpServletRequest request)
  {
    return "";
  }
  
  @RequestMapping(value={"/admin/system/news-list"}, method={org.springframework.web.bind.annotation.RequestMethod.GET})
  private String newsListPage(Model model, HttpServletRequest request, @RequestParam(value="page", required=false, defaultValue="1") int page)
  {
    Page<News> pageModel = new Page();
    pageModel.setPageNo(page);
    pageModel.setPageSize(10);
    List<News> newsList = this.newsService.getNewsList(pageModel);
    
    String pageStr = PagingUtil.getPagelink(page, pageModel.getTotalPage(), "", "admin/system/news-list");
    model.addAttribute("newsList", newsList);
    model.addAttribute("pageStr", pageStr);
    return "news-list";
  }
  
  @RequestMapping(value={"/admin/system/news-add"}, method={org.springframework.web.bind.annotation.RequestMethod.GET})
  private String newsAddPage(Model model, HttpServletRequest request)
  {
    return "";
  }
}

